Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

swagger-router

Package Overview
Dependencies
Maintainers
3
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

swagger-router

An efficient swagger 2 based router with support for multiple APIs. For use in RESTBase.

  • 0.7.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
3
Created
Source

Swagger 2 router

Build
Status

Features

  • O(path element) lookup complexity, monomorphic design with simple fast path.
  • Support for prefix-based 'mounting' of swagger specs. Example: Mount the same spec fragments at /en.wikipedia.org/v1/ and /de.wikipedia.org/v1/.
  • Support for capture of fixed path segments. Example: /{domain:en.wikipedia.org}/v1/. This feature is especially useful in prefixes, as it enables the consistent construction of sensible params.
  • Support for static (purely spec-based) listings. Matching requests ending on a slash are passed an array of matching child paths in the spec in the _ls parameter.

Installation

npm install swagger-router

Usage

var Router = require('swagger-router');
var router = new Router();

// The main requirement is that each spec has a 'paths' member with some URL
// patterns
var swaggerSpec = {
    paths: {
        '/': {
            get: {
                hello: 'A listing'
            }
        },
        '/some/{name}': { // This object is returned as 'value'
            get: {
                hello: 'world'
            }
        }
    }
};

router.addSpec(swaggerSpec);

// Perform some lookups
console.log(router.lookup('/some/title'));
/* 
{
    params: {
        name: 'title'
    },
    value: { get: { hello: 'world' } }
}
*/

// Use arrays internally for speed (no escaping / parsing)
router.lookup(['some','path']);

// Trailing slashes set an additional _ls param:
router.lookup(['']); // equivalent: router.lookup('/'); 
/*
{
    params: {
        _ls: ['some'],
        name: 'title'
    },
    value: { get: { hello: 'A listing' } }
}
*/

URI templating

URIs are represented by URI class, which supports a limited set of features from URI Template RFC 6570.

Supported URI template expressions:

  • Simple string expression {pattern} - on expansion, looks up a variable named pattern in params and substitutes its pct-encoded value. On matching, matches a single element in the path, and sets params.pattern to the path element value.
  • Restricted expression {+pattern} - on expansion, works the same way as simple expression, but doesn't pct-encode reserved characters and ptc-triplets. On matching, matches the whole subpath and writes it's value to params.pattern variable.
  • Optional expression {/pattern} - works the same way as simple expression, but on matching the path element is optional.
  • Fixed expression {pattern:value} - on matching, matches only uris with path element equal to value, and exports value as params.pattern variable. On expansion, substitutes value.

These features are optimised and available with URI.expand(params) method. Additional features are available with request templating.

Request templating

Module exports an efficient templating library under Template class.

Example usage:

var template = new Template({
    method: 'put',
    uri: '/{domain}/{$.request.headers.location}',
    headers: '{$$.merge($.request.headers, {"additional_name": "additional_value"})}'
    body: {
        field_from_req_body: '{field_name}',
        global_reference: '{$.request.headers.header_name}',
        field_with_default_value: '{$$.default($.request.params.param_name, "defaultValue")}'       
    }
});
var request = template.expand({
    request: req,
    additional_context: context
});

Expressions wrapped in curly braces are considered templates, which are resolved to values on template expansion. In case some value cannot be resolved, template is expanded to undefined.

$ references global context (object, passed to the expand method). It can contain arbitrary number of objects, but it must at least contain a request property with an original request.

Short notations are supported, which are resolved to fields of a request part, for example, '{field_name}' in template body would be resolved to '{$.request.body.field_name}'. Short notations in uri would be resolved to $.request.params.

Braced syntax is supported, so it's possible to write templates like '{$.request.body[$.request.params.some_field]}'.

Several utility methods are supported:

  • $$.default(template, defaultValue) - if template is resolved, use it's value, otherwise use defaultValue.
  • $$.merge(template1, template2) - both templates should be evaluated to objects. The result is an object with merged properties, but without overriding.
  • $$.strip(object, properties) - removes field names listed in properties array from an object. properties could also be a string, if a single field should be removed.

Keywords

FAQs

Package last updated on 10 Jun 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc